<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);

class c_ajax_class extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white';
		$rule_action['actions'] = array(
			'get_question_comments',
			'get_answer_comments',
			'log'
		);
		
		if (get_setting('guest_explorer') == 'Y')
		{
			$rule_action['actions'][] = 'discuss';
		}
		
		return $rule_action;
	}

	function setup()
	{
		HTTP::no_cache_header();
	}

	public function uninterested_action()
	{
		$question_id = intval($_POST['question_id']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
			exit();
		}
		
		$this->model('question')->add_question_uninterested($this->user_id, $question_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '操作成功'));
	}

	public function get_focus_users_action()
	{
		$focus_users=$this->model('question')->get_focus_users_by_question($_GET['question_id']);
		
		if (is_array($focus_users))
		{
			foreach ($focus_users as $key => $val)
			{
				$focus_users[$key]['avatar_file'] = get_avatar_url($val['uid'], 'mid', $val['avatar_file']);
			}
		}
		
		H::ajax_json_output($focus_users);
	}

	public function get_answer_users_action()
	{
		H::ajax_json_output($this->model('answer')->get_answer_users_by_question($_GET['question_id']));
	}

	public function answer_attach_upload_action()
	{
		if (get_setting('upload_enable') != 'Y')
		{
			die();
		}
		
		define('IROOT_PATH', get_setting('upload_dir') . '/answer/');
		define('ALLOW_FILE_FIXS', '*');
		
		$this->model('upload')->set_upload_dir('');
		
		$date = date('Ymd');
		
		if (isset($_GET['qqfile']))
		{
			$imagepath = $this->model('upload')->xhr_upload_file(file_get_contents('php://input'), $_GET['qqfile'], $date);
			
			$file_name = $_GET['qqfile'];
		}
		else if (isset($_FILES['qqfile']))
		{
			$imagepath = $this->model('upload')->upload_file($_FILES['qqfile'], $date);
			
			$file_name = $_FILES['qqfile']['name'];
		}
		else
		{
			return false;
		}
		
		if (! $imagepath)
		{
			return false;
		}
		
		$fileinfo = pathinfo($imagepath);
		
		$file_type = $this->model('upload')->get_filetype($imagepath);
		$file_size = $this->model('upload')->get_filesize();
		$sfile_type = ltrim($file_type, ".");
		$img_type_arr = explode(",", 'jpg,png,gif,jpeg,bmp');
		
		// 则生成缩略图
		if (in_array($sfile_type, $img_type_arr))
		{
			foreach (AWS_APP::config()->get('image')->attachment_thumbnail as $key => $val)
			{
				$thumb_file[$key] = $this->model('image')->make_thumb(IROOT_PATH . $imagepath, $val['w'], $val['h'], IROOT_PATH . $fileinfo['dirname'] . '/', $val['w'] . 'x' . $val['h'] . '_' . $fileinfo['basename'], true);
			}
			
			$min_thumb = $thumb_file['square'];
			
			if ($min_thumb)
			{
				$thumb = get_setting('upload_url') . '/answer/' . $date . '/' . $min_thumb;
			}
		
		}
		
		$attach_id = $this->model('answer')->add_answer_attach($file_name, $_GET['attach_access_key'], time(), $fileinfo['basename'], $thumb);
		
		$output = array(
			'success' => true
		);
		
		if ($thumb)
		{
			$output['thumb'] = $thumb;
		}
		else
		{
			$output['class_name'] = $this->model('publish')->get_file_class($sfile_type);
		}
		
		$output['delete_url'] = get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'question/ajax/remove_answer_attach/attach_id-' . base64_encode(H::encode_hash(array(
			'attach_id' => $attach_id, 
			'access_key' => $_GET['attach_access_key']
		)));
		
		echo htmlspecialchars(json_encode($output), ENT_NOQUOTES);
	}

	public function remove_answer_attach_action()
	{
		$attach_info = H::decode_hash(base64_decode($_GET['attach_id']));
		
		$this->model('answer')->remove_answer_attach($attach_info['attach_id'], $attach_info['access_key']);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '附件删除成功'));
	}

	public function add_invite_action()
	{
		$question_id = intval($_GET['question_id']);
		$recipients_uid = intval($_GET['uid']);
		
		if ($question_id == 0 || $recipients_uid == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题或用户不存在"));
		}
		
		if ($recipients_uid == $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "不能邀请自己回复问题"));
		}
		
		if ($this->model('question')->check_question_invite($question_id, 0, $recipients_uid))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "该用户已接受过邀请。"));
		}
		
		if ($this->model('question')->check_question_invite($question_id, $this->user_id, $recipients_uid))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "已邀请过该用户。"));
		}
		
		if ($this->model('question')->add_invite($question_id, $this->user_id, $recipients_uid))
		{
			$this->model('notify')->send($this->user_id, $recipients_uid, notify_class::TYPE_INVITE_QUESTION, notify_class::CATEGORY_QUESTION, $question_id, array(
				'from_uid' => $this->user_id, 
				'question_id' => $question_id
			));
			
			$question_info = $this->model('question')->get_question_info_by_id($question_id);
			
			$this->model('email')->action_email(email_class::TYPE_QUESTION_INVITE, $recipients_uid, get_js_url('/question/' . $question_id), array(
				'user_name' => $this->user_info['user_name'], 
				'question_title' => $question_info['question_content']
			));
			
			H::ajax_json_output(AWS_APP::RSM(null, "1", "邀请用户成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(l, "-1", "邀请失败, 数据库无法写入。"));
		}
	}

	/**
	 * 修改回复内容
	 */
	public function edit_question_answer_action()
	{
		$answer_id = intval($_GET['answer_id']);
		$question_id = intval($_GET['question_id']);
		$answer_content = $_POST['content'];
		
		if (empty($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'answer_content'
			), "-2", "请输入回复内容"));
		}
		
		if (!$answer_info = $this->model('answer')->get_answer_info_by_id($answer_id, 0, ! ($this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator'])))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'answer_content'
			), "-2", "回复不存在"));
		}
		
		if (cjk_strlen($answer_content) < get_setting('answer_length_lower'))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复内容字数不得少于" . get_setting('answer_length_lower') . '个，请返回修改。'));
		}
		
		if (!$this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		$this->model('answer')->update_answer($answer_id, $question_id, $answer_content, 0, $_POST['attach_access_key']);
		
		if ($answer_info['uid'] != $this->user_id AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你没有权限编辑这个回复"));
		}
		
		$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::MOD_ANSWER);
		
		ZCACHE::cleanGroup("question_detail_" . $answer_info['question_id']);
		
		$answer_content = FORMAT::parse_links($answer_content);
		
		$this->model('associate_index')->update_update_time($question_id, 1);
		$this->model('associate_index')->update_update_time($question_id, 3);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'target_id' => $_GET['target_id'], 
			'display_id' => $_GET['display_id']
		), "1", nl2br($answer_content)));
	}

	public function question_attach_edit_list_action()
	{
		$question_id = $_POST['question_id'];
		
		$question_info = $this->model('question')->get_question_info_by_id($question_id);
		
		if (! $question_info)
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '无法获取附件列表'));
		}
		
		if ($question_info['published_uid'] != $this->user_id AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限编辑这个附件列表'));
		}
		
		if ($question_attach = $this->model('question')->get_question_attach($question_id))
		{
			foreach ($question_attach as $attach_id => $val)
			{
				$question_attach[$attach_id]['file_type_class'] = $this->model('publish')->get_file_class(ltrim($val['file_locatin'], '.'));
				
				$question_attach[$attach_id]['delete_link'] = get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'publish/ajax/remove_question_attach/attach_id-' . base64_encode(H::encode_hash(array(
					'attach_id' => $attach_id, 
					'access_key' => $val['access_key']
				)));
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'attachs' => $question_attach
		), 1, ''));
	}

	public function answer_attach_edit_list_action()
	{
		$answer_id = $_POST['answer_id'];
		
		$answer_info = $this->model('answer')->get_answer_info_by_id($answer_id, 0, ! ($this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator']));
		
		if (empty($answer_info))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '回复不存在'));
		}
		
		if ($answer_info['uid'] != $this->user_id AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限编辑这个附件列表'));
		}
		
		if ($answer_attach = $this->model('answer')->get_answer_attach($answer_id))
		{
			foreach ($answer_attach as $attach_id => $val)
			{
				$answer_attach[$attach_id]['file_type_class'] = $this->model('publish')->get_file_class(ltrim($val['file_locatin'], '.'));
				$answer_attach[$attach_id]['delete_link'] = get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'question/ajax/remove_answer_attach/attach_id-' . base64_encode(H::encode_hash(array(
					'attach_id' => $attach_id, 
					'access_key' => $val['access_key']
				)));
			}
		}
				
		H::ajax_json_output(AWS_APP::RSM(array(
			'attachs' => $answer_attach
		), "1", ''));
	}

	public function edit_answer_action()
	{
		$answer_id = intval($_GET['answer_id']);
		
		$answer_info = $this->model('answer')->get_answer_info_by_id($answer_id, 0, ! ($this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator']));
		
		if (empty($answer_info))
		{
			H::js_pop_msg("回复不存在");
		}
		
		if ($answer_info['uid'] != $this->user_id AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::js_pop_msg("你没有权限编辑这个回复");
		}
		
		TPL::assign('answer_info', $answer_info);
		TPL::assign('attach_access_key', md5($this->user_id . time()));
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('question/ajax/edit_answer');
	}

	function agree_answer_action()
	{
		$retval = $this->model('answer')->agree_answer($this->user_id, $_POST['answer_id']);
		
		$ans_info = $this->model('answer')->get_answer_info_by_id($_POST['answer_id'], 0, false);
		
		ZCACHE::cleanGroup('question_detail_' . $ans_info['question_id']);
		
		if ($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'agree'
			)), "1", "赞同发送成功");
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'disagree'
			)), "1", "赞同发送成功");
		}
	}

	public function question_share_txt_action()
	{
		$question_id = intval($_GET['question_id']);
		
		$q_info = $this->model('question')->get_question_info_by_id($question_id);
		
		$q_info['question_content'] = cjk_substr($q_info['question_content'], 100);
		
		$url = get_js_url("/question/" . $question_id . "?source-" . base64_encode($this->model('account')->get_source_hash($this->user_info['email'])));
		
		$data = array(
			'weibo' => "#" . get_setting('site_name') . "# " . $q_info['question_content'] . " " . $url . " (分享自@" . get_setting('site_name') . ")", 
			'mail' => $this->user_info['user_name'] . " 在" . get_setting('site_name') . "分享了一个问题给你： “" . $q_info['question_content'] . "” " . $url, 
			'message' => "我看到一个不错的问题，想和你分享： “" . $q_info['question_content'] . "” " . $url
		);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'share_txt' => $data
		), "1"));
	}

	public function answer_share_txt_action()
	{
		$answer_id = intval($_GET['answer_id']);
		
		$a_info = $this->model('answer')->get_answer_info_by_id($answer_id, 0, false);
		
		$u_info = $this->model('account')->get_users_by_uid($a_info['uid']);
		
		$q_info = $this->model('question')->get_question_info_by_id($a_info['question_id']);
		
		$a_info['answer_content'] = trim(cjk_substr($a_info['answer_content'], 100));
		
		$url = get_js_url("/question/" . $a_info['question_id'] . "?item_id-" . $answer_id . "__source-" . base64_encode($this->model('account')->get_source_hash($this->user_info['email'])) . '#answer_' . $answer_id);
		
		$data = array(
			'weibo' => cjk_substr("#" . get_setting('site_name') . "# " . $q_info['question_content'] . " " . '@' . $u_info['user_name'] . "： " . $a_info['answer_content'], 100) . ' ' . $url . " (分享自@" . get_setting('site_name') . ")", 
			'mail' => $this->user_info['user_name'] . " 在" . get_setting('site_name') . "分享了一个问题给你： “" . $q_info['question_content'] . "” " . $u_info['user_name'] . "： " . cjk_substr($a_info['answer_content'], 300) . ' ' . $url, 
			'message' => "我看到一个不错的问题，想和你分享： “" . $q_info['question_content'] . "” " . $u_info['user_name'] . "： " . cjk_substr($a_info['answer_content'], 300) . ' ' . $url
		);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'share_txt' => $data
		), "1"));
	}

	function discuss_action()
	{
		$question_list = $this->model('question')->get_discuss_list($_GET['page'], get_setting('contents_per_page'), $_GET['sort_type'], $_GET['topic_id'], $this->user_id, $_GET['category'], $_GET['answer_count'], $_GET['day']);
		
		TPL::assign('question_list', $question_list);
		
		if ($_GET['template'] == 'topic')
		{
			TPL::output("question/ajax/list_topic");
		}
		else
		{
			TPL::output("question/ajax/list");
		}
	}

	public function save_answer_comment_action()
	{
		if (! $_GET['answer_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "回复不存在"));
		}
		
		if (trim($_POST['message']) == '')
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "请输入评论内容"));
		}
		
		if((get_setting('comment_limit') > 0) && (cjk_strlen($_POST['message']) > get_setting('comment_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "评论内容字数不得超过" . get_setting('comment_limit') . "个"));
		}
		
		if(!$this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($_POST['message']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		if ($comment_id = $this->model('answer')->insert_answer_comment($_GET['answer_id'], $this->user_id, $_POST['message']))
		{
			$answer = $this->model('answer')->get_answer_info_by_id($_GET['answer_id'], 0, false);
			
			$this->model('notify')->send($this->user_id, $answer['uid'], notify_class::TYPE_ANSWER_COMMENT, notify_class::CATEGORY_QUESTION, $answer['question_id'], array(
				'comment_type' => 2, 
				'from_uid' => $this->user_id, 
				'question_id' => $answer['question_id'], 
				'item_id' => $_GET['answer_id'], 
				'comment_id' => $comment_id
			));
			
			$message_arr = $this->model('question')->parse_at_user($_POST['message'], false, true);
			
			if ($message_arr['user'])
			{
				foreach ($message_arr['user'] as $uid)
				{
					if ($uid == $answer['uid'])
					{
						continue;
					}
					
					$this->model('notify')->send($this->user_id, $uid, notify_class::TYPE_COMMENT_AT_ME, notify_class::CATEGORY_QUESTION, $answer['question_id'], array(
						'comment_type' => 2, 
						'from_uid' => $this->user_id, 
						'question_id' => $answer['question_id'], 
						'item_id' => $_GET['answer_id'], 
						'comment_id' => $comment_id
					));
				}
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'item_id' => $_GET['answer_id'], 
			'type_name' => 'answer'
		), 1, "评论成功"));
	}

	public function get_answer_comments_action()
	{
		$comments = $this->model('answer')->get_answer_comments($_GET['answer_id']);
		
		foreach ($comments as $key => $val)
		{
			$comments[$key]['message'] = FORMAT::parse_links($this->model('question')->parse_at_user($comments[$key]['message']));
			$user_info = $this->model('account')->get_users_by_uid($val['uid']);
			$comments[$key]['user_name'] = $user_info['user_name'];
		}
		
		TPL::assign('comments', $comments);
		
		TPL::output("question/ajax/comments");
	}

	public function save_question_comment_action()
	{
		if (! $_GET['question_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "问题不存在"));
		}
		
		if (trim($_POST['message']) == '')
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "请输入评论内容"));
		}
		
		if((get_setting('comment_limit') > 0) && (cjk_strlen($_POST['message']) > get_setting('comment_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "评论内容字数不得超过" . get_setting('comment_limit') . "个"));
		}
		
		$comment_id = $this->model('question')->insert_question_comment($_GET['question_id'], $this->user_id, $_POST['message']);
		
		if ($comment_id)
		{
			$question = $this->model('question')->get_question_info_by_id($_GET['question_id']);
			
			$this->model('notify')->send($this->user_id, $question['published_uid'], notify_class::TYPE_QUESTION_COMMENT, notify_class::CATEGORY_QUESTION, $question['question_id'], array(
				'comment_type' => 1, 
				'from_uid' => $this->user_id, 
				'question_id' => $question['question_id'], 
				'comment_id' => $comment_id
			));
			
			$message_arr = $this->model('question')->parse_at_user($_POST['message'], false, true);
			
			if ($message_arr['user'])
			{
				foreach ($message_arr['user'] as $uid)
				{
					if ($uid == $question['published_uid'])
					{
						continue;
					}
					
					$this->model('notify')->send($this->user_id, $uid, notify_class::TYPE_COMMENT_AT_ME, notify_class::CATEGORY_QUESTION, $question['question_id'], array(
						'comment_type' => 1, 
						'from_uid' => $this->user_id, 
						'question_id' => $question['question_id'], 
						'comment_id' => $comment_id
					));
				}
			}
		}
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'item_id' => $_GET['question_id'], 
			'type_name' => 'question'
		), 1, "评论成功"));
	}

	public function get_question_comments_action()
	{
		if ($comments = $this->model('question')->get_question_comments($_GET['question_id']))
		{
			foreach ($comments as $key => $val)
			{
				$comments[$key]['message'] = FORMAT::parse_links($this->model('question')->parse_at_user($comments[$key]['message']));
				$user_info = $this->model('account')->get_users_by_uid($val['uid']);
				$comments[$key]['user_name'] = $user_info['user_name'];
			}
		}
		
		TPL::assign('comments', $comments);
		
		TPL::output("question/ajax/comments");
	}

	public function change_vote_action()
	{
		$answer_id = intval($_POST['answer_id']);
		$value = intval($_POST['value']);
		
		$answer_info = $this->model('answer')->get_answer_by_id($answer_id);
		
		if($answer_info['uid'] == $this->user_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "不能对自己发表的回复投票"));
		}
		
		if (! in_array($value, array(
			- 1, 
			1
		)))
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, "投票值错误，无法投票。"));
		}
		
		 
		$reputation_factor=$this->model('account')->get_group_by_id($this->user_info['reputation_group'], 'reputation_factor');
		
		//p($reputation_factor);
		
		$retval = $this->model('answer')->change_answer_vote($answer_id, $value, $this->user_id,$reputation_factor);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	}

	public function cancel_question_invite_action()
	{
		$question_id = intval($_GET['question_id']);
		$recipients_uid = intval($_GET['recipients_uid']);
		
		$this->model('question')->cancel_question_invite($question_id, $this->user_id, $recipients_uid);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, "取消邀请成功"));
	}

	public function question_invite_delete_action()
	{
		$question_invite_id = intval($_POST['question_invite_id']);
		
		$this->model('question')->delete_question_invite($question_invite_id, $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, "删除邀请成功"));
	}

	public function question_answer_rate_action()
	{
		$answer_info = $this->model('answer')->get_answer_by_id($_POST['answer_id']);
		
		if($this->user_id == $answer_info['uid'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '不能评价自己发表的回复'));
		}
		
		if ($this->model('answer')->user_rate($_POST['type'], $_POST['answer_id'], $this->user_id, $this->user_info['user_name']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'add'
			), 1, '回复评价成功'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'action' => 'remove'
			), 1, '撤消评价成功'));
		}
	}

	public function focus_action()
	{
		$question_id = intval($_GET['question_id']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
		}
		
		$retval = $this->model('question')->add_focus_question($question_id);
		
		$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_REQUESTION_FOCUS);
		
		if ($retval)
		{
			ZCACHE::delete("question_focus_q" . $question_id . "_u" . $this->user_id);
			
			ZCACHE::delete("question_get_user_recommend_v2_" . $this->user_id . "_10");
			
			H::ajax_json_output(AWS_APP::RSM(array(
				"type" => $retval
			), "1", "关注成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				"type" => $retval
			), "-1", "关注失败, 请联系管理员"));
		}
	}

	/**
	 *
	 * 添加回复
	 *
	 * @return boolean true|false
	 */
	public function save_answer_action()
	{
		$question_id = intval($_POST['question_id']);
		$answer_content = $_POST['answer_content'];
		
		if (! $question_id)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'sn'
			), "-1", '问题不存在'));
		}
		
		if (! $answer_content)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请输入回复内容"));
		}
		
		//判断是否是问题发起者
		if (get_setting('answer_self_question') == 'N')
		{
			$question_info = $this->model('question')->get_question_info_by_id($_POST['question_id']);
			
			if ($question_info['published_uid'] == $this->user_id)
			{
				H::ajax_json_output(AWS_APP::RSM(null, "-1", "不能回复自己发布的问题，你可以在问题发布后的 " . get_setting('question_edit_time') . " 分钟内修改问题"));
			}
		}
		
		//判断是否已回复过问题
		if ((get_setting('answer_unique') == 'Y') && $this->model('answer')->check_answer_question($_POST['question_id'], $this->user_id))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "一个问题只能回复一次，你可以编辑回复过的回复"));
		}
		
		if (strlen($answer_content) < get_setting('answer_length_lower'))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复内容字数不得少于 " . get_setting('answer_length_lower') . ' 字节，请返回修改。'));
		}
		
		if (!$this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($answer_content))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "你所在的用户组不允许发布站外链接"));
		}
		
		if (human_valid('answer_valid_hour') AND !core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请填写正确的验证码"));
		}
		
		// !注: 来路检测后面不能再放报错提示
		if (!valid_post_hash($_POST['post_hash']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", '表单来路不正确或内容已提交, 请刷新页面重试'));
		}
		
		core_captcha::clear();
		
		if ($answer_id = $this->model('answer')->save_answer($question_id, $answer_content))
		{
			set_human_valid('answer_valid_hour');
			
			$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ANSWER_QUESTION);
			
			ZCACHE::cleanGroup("question_detail_" . $question_id);
			
			if (! $this->model('question')->has_focus_question($question_id, $this->user_id))
			{
				$this->model('question')->add_focus_question($question_id);
				$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_REQUESTION_FOCUS);
			}
			
			$question_info = $this->model('question')->get_question_info_by_id($question_id);
			
			if ($focus_uids = $this->model('question')->get_focus_uid_by_question_id($question_id))
			{
				foreach ($focus_uids as $focus_user)
				{
					$this->model('email')->action_email(email_class::TYPE_NEW_ANSWER, $focus_user['uid'], get_js_url('/question/' . $question_id), array(
						'question_title' => $question_info['question_content']
					));
					
					$data = array(
						'question_id' => $question_id, 
						'from_uid' => $this->user_id, 
						'item_id' => $answer_id
					);
					
					$this->model('notify')->send($this->user_id, $focus_user['uid'], notify_class::TYPE_NEW_ANSWER, notify_class::CATEGORY_QUESTION, $question_id, $data);
				}
			}
			
			//删除回复邀请
			$this->model('question')->answer_question_invite($question_id, $this->user_id);
			
			if ($_POST['attach_access_key'])
			{
				$this->model('answer')->update_answer_attach($answer_id, $_POST['attach_access_key']);
			}
			
			$this->model('draft')->delete_draft($question_id, 'answer', $this->user_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'question_id' => $question_id, 
				'action_type' => $answer_id,
				'url' => get_js_url('/question/id-' . $question_id . '__item_id-' . $answer_id)
			), 1, "问题回复成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "回复问题失败, 请联系管理员"));
		}
	}

	/**
	 * 
	 * 根据话题ID和问题ID，删除关联
	 * 
	 * @return boolean true|false
	 */
	public function delete_topic_action()
	{
// 		if(!$this->user_info['permission']['edit_question'])
// 		{
// 			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限进行操作'));
// 			exit();
// 		}
		
		
		$topic_id = intval($_GET['topic_id']);
		$question_id = intval($_GET['question_id']);
		
		if ($topic_id == 0 || $question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "问题不存在"));
		}
		
		if ($this->model('topic')->delete_topic($topic_id, $question_id, 0))
		{
			ZCACHE::delete("question_topics_" . $question_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'topic_id' => $topic_id
			), 1, "删除话题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "删除话题失败, 请联系管理员"));
		}
	}

	/**
	 * 
	 * 根据问题ID增加话题，并直接关联
	 * 
	 * @return boolean true|false
	 */
	public function save_topic_action()
	{
		$retval = false;
		$topic_title = trim(FORMAT::safe($_GET['topic_title'], true));
		$question_id = intval($_GET['question_id']);
		$topic_lock = intval($_GET['topic_lock']);
		
		if ($question_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'sn'
			), "-1", "问题不存在"));
			exit();
		}
		
		if (empty($topic_title))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'content'
			), "-1", "请输入话题标题"));
		}
		
		if (cjk_strlen($topic_title) < 2)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'type' => 'content'
			), "-1", "话题长度必须大于 2 个字"));
		}
		
		if ($question_id > 0)
		{
			$topic_id = $this->model('topic')->save_topic($question_id, $topic_title, $this->user_id, 0, 0, '', '', $topic_lock, 1);
			$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_TOPIC, $this->user_id);
		}
		
		if (! $topic_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "话题已锁定, 不能添加话题"));
			exit();
		}
		
		$retval = $this->model('question_topic')->save_link($topic_id, $question_id);
		
		if ($retval)
		{
			//更新话题统计
			$this->model('topic')->update_topic_count($topic_id);
			
			ZCACHE::delete("question_topics_" . $question_id);
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'topic_id' => $topic_id
			), 1, "添加话题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "添加话题失败, 请联系管理员"));
		}
	}
	
	function remove_answer()
	{
		$answer_info = $this->model('answer')->get_answer_by_id(intval($_POST['answer_id']));
		
		if(!$answer_info)
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "回复不存在"));
		}
		
		if ($answer_info['uid'] != $this->user_id AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "对不起，你没有删除权限。"));
		}
		
		if($this->model('answer')->remove_answer(intval($_POST['answer_id'])))
		{
			H::ajax_json_output(AWS_APP::RSM(null, 1, "删除问题成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, "删除失败，无法操作数据库。"));
		}
	}
	
	
	function log_action()
	{
		$question_id = $_GET['id'];
		
		$per_page = 10;
		
		$page = $_GET['page'];
		
		$limit = $page * $per_page . ',' . $per_page; 
		
		$action_ids = "101, 102, 103, 401, 405, 108, 109";
		
		$log_list = ACTION_LOG::get_action_by_event_id(false, $question_id, $limit, ACTION_LOG::CATEGORY_QUESTION, $action_ids);
		
		//处理日志记录
		$log_list = $this->model('question')->analysis_log($log_list);
		
		TPL::assign('list', $log_list);
		
		TPL::output('question/ajax/log');
	}
}